<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Ins extends MY_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('User_model');
    }
    public function index()
    {
        $get_type = (int)$this->input->get('type');
        $get_city = $this->input->get('city');
        $get_orderby = $this->input->get('orderby');
        
        $page     = _get_page();
        $pagesize = 100;
        $arrWhere = array('a.nickname is not null'=>'','usertype'=>2,'status>'=>0,'hangup_status'=>$this->show_hangup_status );
        $arrParam = array();
        
        if($get_type)
        {
        	$arrWhere["concat(',',type,',') like "]="'%,".$get_type.",%'";
            $arrParam['type']=$get_type;
        }
        
        $this->load->model('Style_model');
        if ($get_city) {
            if ($get_city == '其他') {
                $cityStr = $this->Style_model->getCityStr(); 
                $arrWhere['(city not in ('] = $cityStr.') or city is null)';
            } else {
                $arrWhere['(city'] = "'".$get_city."市' or city='".$get_city."')";
            } 
            $arrParam['city'] = $get_city;
        }
       
        $orderby = 'visitnum desc,addtime desc';
        if($get_orderby) {
            $arrParam['orderby'] = $get_orderby;
            switch ($get_orderby) {
                case 1:
                    $orderby = 'addtime desc';
                    break;
                case 2:
                    $orderby = 'fansnum desc';
                    break;
                case 3:
                    $orderby = 'be_ordernum desc';
                    break;
                default:
                    $orderby = 'addtime desc';
                    break;
            }
        }
        
        

        $dbprefix = $this->User_model->db->dbprefix;
        $tb = $dbprefix.'user a left join '.$dbprefix.'user_detail b on(a.id=b.userid) left join '.$dbprefix.'user_num c on(a.id=c.userid)';
        $list = $this->User_model->fetch_page($page, $pagesize, $arrWhere, 'a.id,company,nickname,showimg,city,a.usertype', $orderby, $tb);
//         echo $this->User_model->db->last_query();die;
        
        $this->load->model('Userdetail_model');
        $this->load->model('Usernum_model');
        $typeArr = _get_config('type');
        if (!empty($list['rows'])) {
            foreach($list['rows'] as $key=>$ins) {
                // 处理类型展示
                $typeStr = '';
                $infoArr = array();
                $info =  $this->Userdetail_model->get_info_by_userid($ins['id']);
                $insTypeArr = $typeArr[2];
                
                 if (!empty($info['type'])) {
                    if (strstr($info['type'], ',')) {
                        $infoArr= explode(',', $info['type']);
                    } else {
                        $typeStr = $insTypeArr[$info['type']];
                    }
                }
                
                if ($infoArr) {
                    foreach($infoArr as $it) {
                        $typeStr .= ','.$insTypeArr[$it];
                    }
                }
                if (strstr($typeStr, ','))
                {
                	$type_str = ltrim($typeStr, ',');
                	$type_arr = explode(',', $type_str);
                	$type_arr = array_unique($type_arr);
                	$typeStr = implode(',', $type_arr);
                }
                
                $info['type_str'] = $typeStr;
                $list['rows'][$key]['detail_info'] = $info;
                
                //获取点击量
                $numInfo = $this->Usernum_model->get_info_by_userid($ins['id']);
                $list['rows'][$key]['num_info'] = $numInfo;
            }
        } 
       
        
        //分页
        $pagecfg = array();
        $pagecfg['base_url']     = _create_url('ins', $arrParam);
        $pagecfg['total_rows']   = $list['count'];
        $pagecfg['cur_page'] = $page;
        $pagecfg['per_page'] = $pagesize;
        //$this->load->library('pagination');
        $this->pagination->initialize($pagecfg);
        $list['pages'] = $this->pagination->create_links();

        $insType = _get_config('type');
        //公司
        $oSysIns = $insType[2];

        // 获取城市
        $this->load->model('Style_model');
        $oCity = $this->Style_model->getShowCity();
        
        $result = array(
            'list' => $list,
            'oSysIns' => $oSysIns,
            'arrParam' => $arrParam,
            'oCity' => $oCity,
            );


        $this->load->view('ins',$result);
    }

}
